package com.yunzheng.common;

import com.yunzheng.enums.ResultStatuesEnums;
import lombok.Data;

/**
 * <p>
 * controller通用返回值
 * </p>
 *
 * @author 郭思良
 * @create 2020-07-23 15:38
 */

@Data
public class BaseResult {

	/**
	 * 返回值状态码
	 */
	private int code;

	/**
	 * 返回值消息
	 */
	private String message;

	/**
	 * 返回数据
	 */
	private Object data;

	public BaseResult() {
	}

	public BaseResult(ResultStatuesEnums enums, Object data) {
		this.code = enums.getCode();
		this.message = enums.getMessage();
		this.data = data;
	}

	public BaseResult(ResultStatuesEnums enums) {
		this.code = enums.getCode();
		this.message = enums.getMessage();
		this.data = "";
	}

	/**
	 * 自定义状态码 ResultStatuesEnums
	 */
	public BaseResult(int code, String message, Object data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public static BaseResult ok() {
		return BaseResult.ok(ResultStatuesEnums.SUCCESS);
	}

	public static BaseResult ok(ResultStatuesEnums enums) {
		return new BaseResult(enums);
	}

	/**
	 * 返回数据的通用返回结果
	 */
	public static BaseResult ok(Object content) {
		return new BaseResult(ResultStatuesEnums.SUCCESS, content);
	}

	public static BaseResult notOk() {
		return BaseResult.notOk(ResultStatuesEnums.FAIL);
	}

	public static BaseResult notOk(ResultStatuesEnums enums) {
		return new BaseResult(enums);
	}

	public static BaseResult notOk(ResultStatuesEnums enums, Object content) {
		return new BaseResult(enums, content);
	}

	public static BaseResult notOk(Object content) {
		return new BaseResult(ResultStatuesEnums.FAIL, content);
	}

	/**
	 * 根据布尔值判断 返回结果是否成功、失败
	 */
	public static BaseResult optional(boolean condition) {
		return condition ? BaseResult.ok() : BaseResult.notOk();
	}
}
